def last_remaining(n):
    people = list(range(1, n + 1))
    index = 0
    count = 1

    while len(people) > 1:
        if count % 7 == 0 or '7' in str(count):
            people.pop(index)
        else:
            index += 1
        index %= len(people)
        count += 1
    return people[0]
n=int(input())
print(last_remaining(n))